package com.winchain.chainbill.bill.biz.transfer.rx;

import com.shell.bee.base.utils.StringUtil;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.message.keyword.XfeRevRejectKeyword;
import com.shell.governor.modules.message.keyword.XfeRevRejectSplitKeyword;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import java.math.BigDecimal;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.springframework.stereotype.Component;

@Component
public class BillXfeRevRejectMsgSubscriber extends OnErrorNotImplementedSubscriber<BillTranEvent<CbXfe>> {
	public void onApplicationEvent(BillTranEvent<CbXfe> t) {
		String remark = (String) t.getValue("remark");
		Bill<CbXfe> billXfe = t.getBill();
		List<Bill<CbXfe>> child = (List<Bill<CbXfe>>) t.getChildren();
		if ((child == null) || (child.size() == 0)) {
			CbRec cbRec = t.getBill().getRec();
			CbXfe cbXfe = (CbXfe) t.getBill().getSource();

			xfe(cbXfe, cbRec, remark);
		} else {
			split(billXfe, child, remark);
		}
	}

	private void xfe(CbXfe cbXfe, CbRec cbRec, String remark) {
		String template = "MT01404";

		XfeRevRejectKeyword keyWord = new XfeRevRejectKeyword();
		keyWord.setBillAmt(String.valueOf(cbRec.getBillAmt()));

		keyWord.setContactorName(CommonDataUtil.getUserName());

		String xfeDate = SysDateUtil.parseDate(cbXfe.getXfeDate(), "yyyyMMdd", "yyyy-MM-dd");
		keyWord.setXfeDate(xfeDate);
		keyWord.setSellerNm(cbXfe.getSellerNm());

		keyWord.setRemark(remark);

		String senderId = CommonDataUtil.getUserId();

		MessageSender.sendMessageByTmpl(senderId, template, keyWord, cbXfe.getBuyerId());
	}

	private void split(Bill<CbXfe> billXfe, List<Bill<CbXfe>> child, String remark) {
		String template = "MT01405";

		CbXfe xfe = new CbXfe();
		BigDecimal xfeAmt = BigDecimal.ZERO;
		Set<String> sellerNmSet = new LinkedHashSet();
		if ((child != null) && (child.size() > 0)) {
			for (Bill<CbXfe> bill : child) {
				if (StringUtil.notEmpty(((CbXfe) bill.getSource()).getXfeId())) {
					xfe = (CbXfe) bill.getSource();
					xfeAmt = xfeAmt.add(xfe.getBillAmt());
					sellerNmSet.add(xfe.getSellerNm());
				}
			}
		}
		String xfeDate = xfe.getXfeDate();

		String sellerNm = String.join(",", sellerNmSet);
		String buyerId = xfe.getBuyerId();
		CbRec rec = billXfe.getRec();

		XfeRevRejectSplitKeyword keyWord = new XfeRevRejectSplitKeyword();
		keyWord.setBillAmt(String.valueOf(rec.getBillAmt()));

		keyWord.setContactorName(CommonDataUtil.getUserName());

		xfeDate = SysDateUtil.parseDate(xfeDate, "yyyyMMdd", "yyyy-MM-dd");
		keyWord.setXfeDate(xfeDate);

		keyWord.setSellerNms(sellerNm);

		keyWord.setXfeAmt(String.valueOf(xfeAmt));
		keyWord.setRemark(remark);

		String senderId = CommonDataUtil.getUserId();

		MessageSender.sendMessageByTmpl(senderId, template, keyWord, buyerId);
	}
}
